<?php
/**
 * Created by xun at 2020/5/9
 */

namespace app\base;


use app\storage\OnlineUser;

class BaseController extends BaseObject
{

    public $server;
    public $fd;
    public $data;

    /**
     * 获取当前的用户
     * @return array|string
     * @throws Exception
     */
    protected function currentUser()
    {
        $table = OnlineUser::getInstance()->table();
        foreach ($table as $item) {
            if ($item['fd'] == $this->fd) {
                return $item;
            }
        }
        // return OnlineUser::getInstance()->get($this->fd);
    }

    /**
     * @param $fd 客户端连接ID
     * @param \swoole_websocket_server $server
     * @param $data string|array 需要发送的消息
     */
    public function sendMessage($fd, $data)
    {
        $data = is_array($data) ? json_encode($data) : $data;
        $this->server->push($fd, $data);
    }

}